(function(){

	window.JSS = function(sJss){
		
		//判断是否为外部.jss文件
		if(sJss.match(/\.jss$/)){
			sJss = fLoad(sJss);
		}else{
			sJss = fDo(sJss);
		}
		
		fCreateStyle(sJss);

	};
	
	//处理<style type="text/jss">
	var tmp = document.getElementsByTagName("STYLE");
	for(var i=0,l=tmp.length;i<l;i++){
		var bDebug = tmp[i].getAttribute("data-debug") || tmp[i].getAttribute("debug");
		bDebug = (bDebug == "true" || bDebug == "debug") ? true : false;
		if(tmp[i].type=="text/jss"){
			fCreateStyle(fDo(tmp[i].innerHTML,bDebug),tmp[i]);
		}
	}
	
	//处理<script ... data-jss="xxx.jss">
	tmp = document.getElementsByTagName("SCRIPT")
	for(var i=0,l=tmp.length;i<l;i++){
		var bDebug = tmp[i].getAttribute("data-debug") || tmp[i].getAttribute("debug");
		bDebug = (bDebug == "true" || bDebug == "debug") ? true : false;
		var sJss = tmp[i].getAttribute("data-jss") || tmp[i].getAttribute("jss");
		if(sJss){
			fCreateStyle(fLoad(sJss,bDebug));
		}
	}

	/**
	 * 生成style节点
	 *
	 * @param	{string}	处理后的jss代码
	 * @param	{object}	被替换的style节点，如果没有就创建一个
	 * @return	{void}		
	 */
	function fCreateStyle(sJss,oOld){
		var oHead = document.getElementsByTagName('head')[0];
		var oStyle = document.createElement('style');
		oStyle.type = 'text/css';
		if(oStyle.styleSheet){
			oStyle.styleSheet.cssText = sJss;
		}else{
			oStyle.innerHTML = sJss;
		}
		if(oOld){
			oHead.replaceChild(oStyle,oOld);
		}else{
			oHead = document.getElementsByTagName('head')[0];
			oHead.appendChild(oStyle);
		}
		
	}
	
	/**
	 * 处理jssvar和jssfunc
	 *
	 * @param	{string}		待处理的jss代码
	 * @return	{string}		处理后的jss代码
	 */
	function fDoVarsAndFuncs(sJss){
		var oVars = {};
		var oFuncs = {};
		
		//内置常量和函数
		oFuncs["$jss_cs"] = {"dftValue":["12px","arial,'\\5B8B\\4F53'"],"body":"/*Classtyle2.0.0.beta*/html{-overflow-y:scroll}body{color:#000;background:#fff;font-size:$jss_0;line-height:1.666;-webkit-text-size-adjust:none}body,textarea{font-family:$jss_1}body,h1,h2,h3,h4,h5,h6,ul,ol,li,form,p,dl,dt,dd,blockquote,legend,table,th,td,fieldset,menu,pre{margin:0;padding:0}table,th,td,img,fieldset{border:0}address,cite,em,code,var,dfn,ins,i,th,caption{font-style:normal;text-decoration:none}h1,h2,h3,h4,h5,h6,th{font-size:100%;font-weight:normal}input,select,button,textarea,table{margin:0;font-family:inherit;font-size:100%}abbr,acronym{border:0;font-variant:normal}q:before,q:after{content:''}ul,ol{list-style:none}table{border-collapse:collapse;border-spacing:0}th,caption{text-align:left}.cs-clear{+zoom:1}.cs-clear:before,.cs-clear:after{clear:both;content:'.';display:block;font-size:0!important;height:0!important;overflow:hidden!important;margin:0!important;padding:0!important;border:0!important}.cs-fl{float:left;display:inline}.cs-fr{float:right;display:inline}.cs-tl{text-align:left}.cs-tc{text-align:center}.cs-tr{text-align:right}.cs-noime{ime-mode:disabled}.cs-ib{display:inline-block;+zoom:1;vertical-align:middle}.cs-hideText{line-height:9999px!important;overflow:hidden;font-size:0!important}.cs-br{word-break:break-all;word-wrap:break-word}.cs-nobr{white-space:nowrap}.cs-qmode *,.cs-qmbox{-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box}.cs-qmode td{-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box}.cs-ellipsis{height:1.666em;overflow:hidden;+white-space:nowrap;+text-overflow:ellipsis}.cs-ellipsis_text{position:relative;padding-right:1em;max-height:200%;overflow:hidden;+white-space:nowrap;+text-overflow:ellipsis}.cs-ellipsis_text:after{content:'...';font-family:tahoma;display:inline;position:absolute;right:0;bottom:1.666em}.cs-check input,input.cs-check{width:16px;height:16px;padding:0;margin:0;overflow:hidden;vertical-align:middle}.cs-select,.cs-input,.cs-button,.cs-label{margin-right:.3em;vertical-align:middle}.cs-label{white-space:nowrap;-zoom:1}.cs-label .cs-check{margin-right:3px;-vertical-align:baseline}.cs-button{line-height:inherit;+line-height:1.3;padding:0 1em;+overflow:visible;cursor:pointer}.cs-input{width:12em;padding:.22em 0;height:1em;line-height:1em}.cs-page-wide{width:950px}.cs-page-narrow{width:790px}.cs-txt-serif,.cs-txt-song,.cs-txt-chn{font-family:'\\5B8B\\4F53'}.cs-txt-sans,.cs-txt-a,.cs-txt-eng{font-family:arial}.cs-txt-t,.cs-txt-chnEng{font-family:tahoma}.cs-txt-v{font-family:verdana}.cs-txt-monospace,.cs-txt-c,.cs-txt-code{font-family:consolas,'Courier New',monospace}.cs-txt-hei{font-family:'\\9ED1\\4F53'}.cs-txt-yh{font-family:'\\5FAE\\8F6F\\96C5\\9ED1',arial}.cs-txt-xs{font-size:10px!important}.cs-txt-s{font-size:12px!important}.cs-txt-m{font-size:14px!important}.cs-txt-l{font-size:16px!important}.cs-txt-xl{font-size:20px!important}.cs-txt-b{font-weight:bold!important}.cs-txt-u{text-decoration:underline!important}.cs-txt-i{font-style:italic!important}.cs-txt-del{text-decoration:line-through!important}.cs-txt-mark,.cs-txt-succ{color:#090}.cs-txt-impt,.cs-txt-err{color:#c00}.cs-txt-tips{color:#666}.cs-txt-disabd{color:#999}a.cs-txt-disabd{text-decoration:none;cursor:default}.cs-txt-dft{font-weight:normal!important;text-decoration:none!important;font-style:normal!important}"};
		oFuncs["$jss_css3"] = {"dftValue":[""],"body":"-webkit-<$jss_0>;-moz-<$jss_0>;-o-<$jss_0>;<$jss_0>;"};
		oFuncs["$jss_br"] = {"dftValue":["5px"],"body":"-webkit-border-radius:$jss_0;-moz-border-radius:$jss_0;border-radius:$jss_0"};
		oFuncs["$jss_bs"] = {"dftValue":["0","0","5px","#ccc"],"body":"-webkit-box-shadow:$jss_0 $jss_1 $jss_2 $jss_3;-moz-box-shadow:$jss_0 $jss_1 $jss_2 $jss_3;box-shadow:$jss_0 $jss_1 $jss_2 $jss_3"};
		oFuncs["$jss_op"] = {"dftValue":["0.8"],"body":"opacity:$jss_0;filter:alpha(opacity=$jss_0*100 )"};
		
		//匹配jssfunc定义：$XXX($XXX){XXX}
		var rFuncs = /(\$[\w_]*?)\s*\((\$[^\r\n\(\)\[\]\{\}]*?)?\)\s*\{(([^\{\}]|(?:\{(?:[^\{\}]|(?:\{(?:[^\{\}]|(?:\{(?:[^\{\}]|(?:\{(?:[^\{\}]|(?:\{(?:[^\{\}]|(?:\{(?:[^\{\}]|(?:\{(?:[^\{\}]|(?:\{(?:[^\{\}]|(?:\{[^\{\}]*?\}))*?\}))*?\}))*?\}))*?\}))*?\}))*?\}))*?\}))*?\}))*?)\}/g;
		//匹配jssvar定义：$XXX=XXX;
		var rVars = /(\$[\w_]*?)\s*\=\s*((\")?(.*?)\3)\s*?;/g;
		
		//找jssfunc定义，保存到funcs中
		var temp = sJss.match(rFuncs);
		sJss = sJss.replace(rFuncs,"");//删掉定义
		if(temp){
			for(var i=0,m=temp.length;i<m;i++){
				temp[i] = temp[i].replace(rFuncs,"$1||$3||$2");//从匹配到的func定义中拿到func名，func主体和func参数
				var aVar = temp[i].split("||");
				if(aVar[2]!=""){
					var aArgs = _fGetParam(aVar[2]);
					var aDftValue = [];
					for(var j=0,am=aArgs.length;j<am;j++){
						//匹配jssfunc参数中的默认值定义
						if(aArgs[j].match(/=/)){
							aDftValue[j] = aArgs[j].replace(/.*?=\s*?(.*?)$/g,"$1");
							aArgs[j] = aArgs[j].replace(/(.*?)\s*?=.*?$/g,"$1");
						}else{
							aDftValue[j]="";
						}
						
						//将jssfunc主体中的参数调用的参数名替换为$jss_X的形式避免与全局的jssvar重名
						var rArgs = new RegExp("\\"+aArgs[j]+"(?=([\\W]|$))","g");
						aVar[1] = aVar[1].replace(rArgs,"$$jss_"+j);
					}
				}else{
					var aDftValue = null;
				}

				oFuncs[aVar[0]] = {"dftValue":aDftValue,"body":aVar[1]};
			}
		}
		
		//找jssvar定义，保存到oVars中
		temp = sJss.match(rVars);
		sJss = sJss.replace(rVars,"");//删掉定义
		if(temp){
			for(var i=0,m=temp.length;i<m;i++){
				temp[i] = temp[i].replace(rVars,"$1|$2");
				var aVar = temp[i].split("|");
				oVars[aVar[0]] = _fCompute(aVar[1],oVars);//计算出定义的值
			}
		}
		
		
		//将jss中调用的jssvar和jssfunc替换为定义的值
		sJss = _fReplaceFuncs(oFuncs,sJss);
		sJss = _fReplaceVars(oVars,sJss);

		
		/**
		 * 取jssfunc的参数
		 *
		 * @param	{string}	$func(...)括号中的部分
		 * @return	{array}		参数数组
		 */
		function _fGetParam(sParam){
		
			//如果参数中没有","的情况就直接用,分成数组，否则做替换
			if(sParam.match(/\"[^\"]+?[,][^\"]+?\"/)){
				var aParam = [];
				var tmp =[];
				sParam = sParam.replace(/\"[^\"]*?\"/g,function(){
					tmp.push(arguments[0]);
					return "$jss_tmp";
				});
				
				aParam = sParam.split(",");
				for(var i=0,j=0,l=aParam.length;i<l;i++){
					aParam[i] = aParam[i].replace("$jss_tmp",function(){var sValue = tmp[j];j++;return sValue;});
				}
				
				return aParam;
			}else{
				return sParam.split(",");
			}
		}
		
		/**
		 * 处理jssfunc主体的代码
		 *
		 * @param	{string}	jssfunc的函数名
		 * @param	{array}		jssfunc的参数值数组
		 * @return	{string}	处理后的jss代码
		 */
		function _fCallFunc(sFuncName,aArgsValue){
			var oArgs = {};
			var sFuncName = arguments[1];
			var aDftValue = oFuncs[sFuncName].dftValue;
			var aArgsValue = _fGetParam(arguments[2].replace(/\s*?,\s*?/g,","));
			var sFuncBody = oFuncs[sFuncName].body;
			
			sFuncBody = sFuncBody.replace(/\}[\r\n\s]*;/g,"}");
			
			//如果设有默认值，参数为空时使用默认值
			if(aDftValue!=null){
				for(var i=0,m=aDftValue.length;i<m;i++){
					oArgs["$jss_"+i] = aDftValue[i];
					if(aArgsValue[i] != null && aArgsValue[i] != ""){
						if(aDftValue[i] != aArgsValue[i]){
							oArgs["$jss_"+i] = aArgsValue[i];
							if(oArgs["$jss_"+i].match("$")){
								oArgs["$jss_"+i] = _fCompute(oArgs["$jss_"+i],oVars);
							}
						}
					}
				}
			}
			
			for(var sVarName in oArgs){
				oVars[sVarName] = oArgs[sVarName];
			}

			//将jssfunc函数体中调用的参数、全局jssvar和jssfunc替换为定义的值
			sFuncBody = _fReplaceVars(oVars,sFuncBody);
			//sFuncBody = _fReplaceVars(oVars,sFuncBody);
			sFuncBody = _fReplaceFuncs(oFuncs,sFuncBody);

			return sFuncBody;
		}
		
		
		/**
		 * 替换jssvar为处理后的值
		 *
		 * @param	{object}	jssvar定义
		 * @param	{string}	待处理的jss代码
		 * @return	{string}	处理后的jss代码
		 */
		function _fReplaceVars(oVars,sJss){
			for(var sVarName in oVars){
				
				//<$var>
				sJss = sJss.replace(new RegExp("\\<\\"+sVarName+"\\>","g"),(oVars[sVarName].replace(/(^\")|(\"$)/g,"")).replace(/\\\"/g,"\""));
			
				//如果有运算符就计算得出结果，否则直接替换
				if(sJss.match(new RegExp("([\\+\\-\\*\\/]\\"+sVarName+")|(\\"+sVarName+"[\\+\\-\\*\\/])"))){
					
					var aExp = sJss.match(new RegExp("[^;\\{:\\s]*\\"+sVarName+".*?(?=([,;:\\}]|$))","g"));
					for(var i=0,l=aExp.length;i<l;i++){
						if(aExp[i].match(/\s/)){
							var aSubExp = [];
							var aExpString = [];
							var aExpValue = [];
							var nTmp = 0;
							aExpValue[i] = "";
							
							aExpString[i] = aExp[i].replace(/\"[^\"]+?\"/g,function(){
								var sName = "$jss_tmp"+(nTmp++);
								oVars[sName] = arguments[0];
								return sName;
							});
							
							aSubExp = aExpString[i].split(" ");
							for(var j=0,m=aSubExp.length;j<m;j++){
								aSubExp[j] = aSubExp[j].replace(/(url|AlphaImageLoader)\((src\=)?(.+)\)/,function(){return arguments[1]+"("+_fCompute(arguments[3],oVars)+")";});
								aSubExp[j] = _fCompute(aSubExp[j],oVars);
								aExpValue[i] += aSubExp[j] + " ";
							}
							sJss = sJss.replace(aExp[i],aExpValue[i]);

						}else{
							sJss = sJss.replace(aExp[i],function(){
								var sValue;
								sValue = arguments[0].replace(/(url|AlphaImageLoader)\((src\=)?(.+)\)/,function(){return arguments[1]+"("+arguments[2]+_fCompute(arguments[3],oVars)+")";});
								sValue = _fCompute(sValue,oVars);
								return sValue;
							});
						}
					}
				}else{
					sJss = sJss.replace(new RegExp("\\" + sVarName + "(?=([,\\s:;\\{\\}\\)\\[\\]]|$))","g"),(oVars[sVarName].replace(/(^\")|(\"$)/g,"")).replace(/\\\"/g,"\""));//原样输出的字符串需要去掉定义中的双引号，把转义的引号恢复成引号
				}
			}
			return sJss;
		}
		
		
		/**
		 * 替换jssfunc为处理后的代码
		 *
		 * @param	{object}	jssfunc的定义
		 * @param	{string}	待处理的jss代码
		 * @return	{string}	处理后的jss代码
		 */
		function _fReplaceFuncs(oFuncs,sJss){
			for(var sFuncName in oFuncs){
				sJss = sJss.replace(new RegExp("(\\"+sFuncName+")\\(((?:(?:[^()]|(?:\\([^()]*\\)))*)*)\\);?(?=(\\s|;|\\}|$))","g"),_fCallFunc);
			}
			return sJss;
		}
		
		
		/**
		 * 计算jssvar的值
		 *
		 * @param	{string}	jss表达式
		 * @param	{object}	jssvar定义
		 * @return	{string}	计算后的结果
		 */
		function _fCompute(sExp,oVars){
			//如果包含在双引号中就不计算原样输出
			if(sExp.match(/^\"[^\"]*?\"$/) == null){
				//如果有jssvar就先取值
				if(sExp.match(/\$/g)){
					sExp = sExp.replace(/(\$.*?)(?=(?:[\+\-\*\/\s:;\{\}\(\)]|$))/g,function(){return _fGetValue(arguments[0],oVars)});
				}
				try{
					//如果有运算符就计算，并将px、pt等单位去掉，计算出结果后再加上
					if(sExp.match(/[\+\-\*\/]/)){
					
						if(sExp.match(/[^\$]?[0-9]+(px|pt|em|cm|m|deg|s|%)/)){
							var sUnit = sExp.match(/[^\$]?[0-9]+(px|pt|em|cm|m|deg|s|%)/)[1];
								
							if(sExp.match(/\"/)){
								sExp = sExp.replace(/(^|[^\"])([0-9]+)(px|pt|em|cm|m|deg|s|%)($|[^\"])/g,"$1\"$2$3\"$4");
								sExp = eval(sExp);
							}else{
								sExp = sExp.replace(/([^\$]?[0-9]+)(?:px|pt|em|cm|m|deg|s|%)/g,"$1");
								sExp = eval(sExp) + sUnit;
							}
						}else{
							sExp = eval(sExp).toString();
						}
					}
				}catch(e){
				}
			}
			/*
			else{
				sExp = sExp.replace(/(^\")|(\"$)/g,"");
			}
			*/
			return sExp;
		}

		
		/**
		 * 从jssvar定义中取值
		 *
		 * @param	{string}	jssvar变量名
		 * @param	{object}	jssvar定义
		 * @return	{string}	jssvar值
		 */
		function _fGetValue(sVarName,oVars){
			try{
				//如果还有jssvar就继续取值
				if(oVars[sVarName].match(/\$/)){
					return _fGetValue(sVarName,oVars[sVarName]);
				}else{
					return oVars[sVarName];
				}
			}catch(e){
				return "";
			}
		}

		return sJss;
	}
	
	/**
	 * 处理继承
	 *
	 * @param	{string}	待处理的jss代码
	 * @return	{string}	处理后的jss代码
	 */
	 function fDoInherit(sJss){
			
			var oInherit = {};
			var oFullInherit = {};
			
			sJss = sJss.replace(/([\w\-_,\.\s]+)\s*(\:\:?)\s*(\.[\.\w\-_,]+)\s*(?=\{|\s)/g,function(){
				var sSelf= arguments[1].replace(/^\s+/g,"");
				var aBase = arguments[3].split(",");
				
				if(arguments[2]==":"){
					oInherit[sSelf]= aBase;
				}else{
					oFullInherit[sSelf]= aBase;
				}
				return sSelf;
			});
			
			for(var sSelf in oFullInherit){
				for(var i=0,l=oFullInherit[sSelf].length;i<l;i++){
					sJss = sJss.replace(new RegExp("(^|\}|,)[;]?([^\{\}]*?\\"+oFullInherit[sSelf][i]+"(?=[\\-\\_\\W])[^\\{\\}]*?)(?=[\\{,])","g"),function(){
						
						var sSel = arguments[2].replace(new RegExp("\\"+oFullInherit[sSelf][i]+"(?=[\\-\\_\\W]|$)","g"),sSelf);
						return arguments[1]+sSel+","+arguments[2];
						
					});
				}
			}
			
			for(var sSelf in oInherit){
				for(var i=0,l=oInherit[sSelf].length;i<l;i++){
					sJss = sJss.replace(new RegExp("(^|\\}|,)[;]?[\\r\\n\\s]*(\\"+oInherit[sSelf][i]+"([\\:\\s]+?[^\\{\\}]*?)?)(?=[\\{,])","g"),function(){
						var sSel = arguments[2].replace(new RegExp("^\\"+oInherit[sSelf][i]+"(?=[\\W]|$)","g"),sSelf);
						
						return arguments[1]+sSel+","+arguments[2];
					});
				}
			
				//sJss = fDoGroup(sJss);
			}
			
			return sJss;
	 }

	
	/**
	 * 处理封装
	 *
	 * @param	{string}	待处理的jss代码
	 * @return	{string}	处理后的jss代码
	 */
	 
	 function fDoNest(sJss){

		var rNest = /([^\{\}\(\);]+)\s*\{((?:[^\{\}]+?\{[^\{\}]*?\}[^\{\}]*?)+?)\}/g;
		sJss = sJss.replace(rNest,function(){
			return _fExpand(arguments[1],arguments[2]);
		});

		if(sJss.match(rNest)){
			sJss = fDoNest(sJss);
		}
		
		/**
		 * 展开嵌套
		 *
		 * @param	{string}	外部的选择器
		 * @param	{string}	内部的代码
		 * @return	{string}	处理后的内部代码
		 */
		function _fExpand(sOuter,sInner){
			
			sOuter = sOuter.replace(/[\r\n\t]+/g,"");
			
			//sTmp = sOuter;
			//sOuter = sOuter.replace(/\:\..*$/g,"");
			
			if(sOuter.match(/,/)){
				sOuter = "(" + sOuter + ")";
			}
			
			sInner = sInner.replace(/([^\r\n\t\{\},]+?,)(?=[^\}]+?\{)/g,sOuter+" $1");
			sInner = sInner.replace(/([^\r\n\t\{\},]+?)(?=\{)/g,sOuter+" $1");
			sInner = sInner.replace(/\s*?\$(?:this)?\s*?(?=\{|,|\:)/g,"");
			sInner = sInner.replace(/\s([\-\_][^\}\d]*?(?=[^;][\s\{,]))/g,"$1");
			sInner = fDoGroup(sInner);
			
			return sInner;
		}
		
		return sJss;
	 }

	/**
	 * 处理Hack
	 *
	 * @param	{string}	待处理的jss代码
	 * @return	{string}	处理后的jss代码
	 */
	function fDoHack(sJss){
		
		var sUA = navigator.userAgent.toLowerCase();
			
		if(sJss.match(/\-\!?(?:ie|cr|sf|wk|ff|op)[\<\>]?[\=]?\d*?\-(?=[^\{]+?\})/)){
			var aBrowser = _fCheckBrowser().split(":");

			var oRegex = {
				"xn":"(?:"+aBrowser[0]+")(?:"+aBrowser[1]+")?",				//-xn-
				"nxn":"\\!(?:"+aBrowser[0]+")(?!"+aBrowser[1]+")\\d+?",		//-!xn-
				"nx":"\\!(?!"+aBrowser[0]+").+?"										//-!x-
			};
			
			var rBrowser = new RegExp("\\-(?:("+oRegex.xn+")|("+oRegex.nxn+")|("+oRegex.nx+"))\\-(?=[^\\{]+?\\})","g");
			sJss = sJss.replace(rBrowser,"");
		}
		
		sJss = sJss.replace(/\-\?(\w+?)\-(?=[^\{]+?\})/g,function(){
			if(sUA.match(new RegExp("(?:^|\\W)" + arguments[1] + "(?:$|\\W)"))){
				return "";
			}else{
				return arguments[0];
			}
		});

		function _fCheckBrowser(){
			if(sUA.match("msie")){
				return sUA.replace(/.*?msie\s(\d+?)\..*/g,"ie:$1");
			}else if(sUA.match("chrome")){
				return sUA.replace(/.*?chrome\/(\d+?)\..*/g,"cr|wk:$1");
			}else if(sUA.match("safari")){
				return sUA.replace(/.*?(version|safari)\/(\d+?)\..*/g,"sf|wk:$1");
			}else if(sUA.match("applewebkit")){
				return sUA.replace(/.*?applewebkit\/(\d+?)\..*/g,"wk:$1");
			}else if(sUA.match("firefox")){
				return sUA.replace(/.*?firefox\/(\d+?)\..*/g,"ff:$1");
			}else if(sUA.match("opera")){
				return sUA.replace(/.*?version\/(\d+?)\..*/g,"op:$1");
			}else{
				return "";
			}
		}

		return sJss;
	}
	
	/**
	 * 处理jss组选择器
	 *
	 * @param	{string}	待处理的jss代码
	 * @return	{string}	处理后的jss代码
	 */
	function fDoGroup(sJss){
		
		sJss = sJss.replace(/\(([^\(\)\{\}]+?)\)\s*?(\s|\>|\+|\~|\-|\_|\:\:?)\s*?\(([^\(\)\}]+?)\)(?=[\{,])/g,function(){return _fExpand(arguments[1],arguments[2],arguments[3]);});	//(i,j,k) (x,y,z){}
		sJss = sJss.replace(/([^\{\}\(\)\r\n\s,]+?)\s*?(\s|\>|\+|\~|\-|\_|\:\:?)\s*?\(([^\(\)\}]+?)\)(?=[\{,])/g,function(){return _fExpand(arguments[1],arguments[2],arguments[3]);});	//n (x,y,z){}
		sJss = sJss.replace(/\(([^\(\)\{\}]+?)\)(\s|\>|\+|\~|\-|\_|\:\:?)([^\$][^};]*?)\s*?(?=[\{,])/g,function(){return _fExpand(arguments[1],arguments[2],arguments[3]);});	//(x,y,z):n{}

		sJss = sJss.replace(/(^|\W)\(([^\(\)]+?)\)(?=[^\{\}]*\{)/g,"$1$2");	//去掉无用的括号：table (td){}
		
		function _fExpand(sBefore,sSymbol,sAfter){
			var aBefore = sBefore.split(",");
			var aAfter = sAfter.split(",");
			var sResult = "";
			for(var i=0,l=aBefore.length;i<l;i++){
				for(var j=0,m=aAfter.length;j<m;j++){
					sResult += aBefore[i] + sSymbol + aAfter[j] + ",";
				}
			}
			
			sResult = sResult.replace(/,$/,"");
			
			return sResult;
		}
		
		return sJss;
	}

	
	/**
	 * 处理jss
	 *
	 * @param	{string}		待处理的jss代码
	 * @param	{boolean}	debug模式
	 * @return	{string}		处理后的jss代码
	 */
	function fDo(sJss,bDebug){

		//去掉无用字符
		if(!bDebug){
			sJss = sJss.replace(/(\r|\n)/g,"");			
			sJss = sJss.replace(/\t+/g,"\t");
		}
		sJss = sJss.replace(/\/\*(.|\r|\n)*?\*\//g,"");

		
		
		sJss = fDoVarsAndFuncs(sJss);
		
		//@media特殊处理
		var rMedia = /@media.+?\{/g;
		var nMediaCount = 0;
		var aMedia = [];
		sJss = sJss.replace(rMedia,function(){
			var tmp = "$jss_media"+nMediaCount+";";
			nMediaCount++;
			aMedia.push(arguments[0]);
			return tmp;
		});
		
		
		sJss = fDoHack(sJss);
		sJss = fDoGroup(sJss);
		sJss = fDoNest(sJss);
		sJss = fDoInherit(sJss);
		
		//还原@media
		for(var i=0,l=aMedia.length;i<l;i++){
			//alert(aMedia[i]);
			sJss = sJss.replace("$jss_media"+i+";",aMedia[i]);
		}

		//容错处理
		sJss = sJss.replace(/(?:\})[\s\r\n]*?;+/g,"}");
		
		return sJss;
	}

	/**
	 * 从url取得jss文件并处理
	 *
	 * @param	{string}		jss文件url
	 * @param	{boolean}	debug模式
	 * @return	{string}		处理后的jss代码
	 */
	function fLoad(sUrl,bDebug){
		var oXmlHttp;
		var sJss;

		try{
			oXmlHttp=new XMLHttpRequest();
		}catch(e){
			try{
				oXmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					oXmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){
					return false;
				}
			}
		}

		try{
			oXmlHttp.open("GET",sUrl,false);
			oXmlHttp.send(null);
			sJss = oXmlHttp.responseText;
		}catch(e){
			sJss = "";
		}

		return fDo(sJss,bDebug);

	}

})();